home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / rzsz0593.zip / SZ.C < prev    next >
C/C++ Source or Header  |  1993-05-03  |  34KB  |  1,569 lines

  1. #define VERSION "3.23 5-03-93"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*
  5.  **************************************************************************
  6.  *
  7.  * sz.c By Chuck Forsberg,  Omen Technology INC
  8.  *    Copyright 1993 Omen Technology Inc All Rights Reserved
  9.  *
  10.  * 
  11.  *    This version implements numerous enhancements including ZMODEM
  12.  *    Run Length Encoding and variable length headers.  These
  13.  *    features were not funded by the original Telenet development
  14.  *    contract.
  15.  * 
  16.  *  This software may be freely used for educational (didactic
  17.  *  only) purposes.  This software may also be freely used to
  18.  *  support file transfer operations to or from licensed Omen
  19.  *  Technology products.  Use with other commercial or shareware
  20.  *  programs (Crosstalk, Procomm, etc.) REQUIRES REGISTRATION.
  21.  *
  22.  *  Any programs which use part or all of this software must be
  23.  *  provided in source form with this notice intact except by
  24.  *  written permission from Omen Technology Incorporated.
  25.  *
  26.  * 
  27.  * Use of this software for commercial or administrative purposes
  28.  * except when exclusively limited to interfacing Omen Technology
  29.  * products requires a per port license payment of $20.00 US per
  30.  * port (less in quantity, see mailer.rz).  Use of this code by
  31.  * inclusion, decompilation, reverse engineering or any other means
  32.  * constitutes agreement to these conditions and acceptance of
  33.  * liability to license the materials and payment of reasonable
  34.  * legal costs necessary to enforce this license agreement.
  35.  *
  36.  *
  37.  *        Omen Technology Inc
  38.  *        Post Office Box 4681
  39.  *        Portland OR 97208
  40.  *
  41.  *    This code is made available in the hope it will be useful,
  42.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  43.  *    DAMAGES OF ANY KIND.
  44.  *
  45.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  46.  */
  47.  
  48. char *Copyrsz = "Copyright 1993 Omen Technology Inc All Rights Reserved";
  49.  
  50. char *substr(), *getenv();
  51.  
  52. #define LOGFILE "/tmp/szlog"
  53. #define LOGFILE2 "szlog"
  54. #include <stdio.h>
  55. #include <signal.h>
  56. #include <ctype.h>
  57. #include <errno.h>
  58. extern int errno;
  59. #define STATIC
  60.  
  61. #define PATHLEN 256
  62. #define OK 0
  63. #define FALSE 0
  64. #ifdef TRUE
  65. #undef TRUE
  66. #endif
  67. #define TRUE 1
  68. #define ERROR (-1)
  69. /* Ward Christensen / CP/M parameters - Don't change these! */
  70. #define ENQ 005
  71. #define CAN ('X'&037)
  72. #define XOFF ('s'&037)
  73. #define XON ('q'&037)
  74. #define SOH 1
  75. #define STX 2
  76. #define EOT 4
  77. #define ACK 6
  78. #define NAK 025
  79. #define CPMEOF 032
  80. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  81. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  82. #define TIMEOUT (-2)
  83. #define RCDO (-3)
  84. #define GCOUNT (-4)
  85. #define RETRYMAX 10
  86.  
  87.  
  88. #define HOWMANY 2
  89. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  90. unsigned Baudrate = 9600;        /* Default, set by first mode() call */
  91. STATIC unsigned Effbaud = 9600;
  92. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  93. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  94. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  95. STATIC unsigned long Lrxpos;    /* Receiver's last reported offset */
  96. STATIC int errors;
  97. char endmsg[80] = {0};    /* Possible message to display on exit */
  98.  
  99. #include "rbsb.c"    /* most of the system dependent stuff here */
  100.  
  101. #include "crctab.c"
  102.  
  103. STATIC int Filesleft;
  104. STATIC unsigned long Totalleft;
  105.  
  106. /*
  107.  * Attention string to be executed by receiver to interrupt streaming data
  108.  *  when an error is detected.  A pause (0336) may be needed before the
  109.  *  ^C (03) or after it.
  110.  */
  111. #ifdef READCHECK
  112. STATIC char Myattn[] = { 0 };
  113. #else
  114. #ifdef USG
  115. STATIC char Myattn[] = { 03, 0336, 0 };
  116. #endif
  117. #endif
  118.  
  119. FILE *in;
  120.  
  121. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  122.  
  123. #ifdef TXBSIZE
  124. #define TXBMASK (TXBSIZE-1)
  125. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  126. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  127. #else
  128. STATIC char txbuf[1024];
  129. #endif
  130. STATIC unsigned long vpos = 0;        /* Number of bytes read from file */
  131.  
  132. STATIC char Lastrx;
  133. STATIC char Crcflg;
  134. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  135. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  136. STATIC int Fullname=0;        /* transmit full pathname */
  137. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  138. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  139. STATIC int firstsec;
  140. STATIC int errcnt=0;        /* number of files unreadable */
  141. STATIC int blklen=128;        /* length of transmitted records */
  142. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  143. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  144. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  145. STATIC int Totsecs;        /* total number of sectors this file */
  146. STATIC int Filcnt=0;        /* count of number of files opened */
  147. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  148. STATIC int Tframlen = 0;    /* Override for tx frame length */
  149. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  150. STATIC int Rxflags = 0;
  151. STATIC unsigned long bytcnt;
  152. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  153. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  154. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  155. STATIC int Lskipnocor;
  156. STATIC char Lztrans;
  157. STATIC int Command;        /* Send a command, then exit. */
  158. STATIC char *Cmdstr;        /* Pointer to the command string */
  159. STATIC int Cmdtries = 11;
  160. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  161. STATIC int Exitcode;
  162. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  163.             /* 2= Character transparency test */
  164. STATIC char *qbf=
  165.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  166. STATIC unsigned long Lastsync;    /* Last offset to which we got a ZRPOS */
  167. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd here */
  168.  
  169. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  170. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  171.  
  172.  
  173. /* called by signal interrupt or terminate to clean things up */
  174. void
  175. bibi(n)
  176. {
  177.     canit(); fflush(stdout); mode(0);
  178.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  179.     if (n == SIGQUIT)
  180.         abort();
  181.     if (n == 99)
  182.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  183.     exit(3);
  184. }
  185.  
  186. /* Called when ZMODEM gets an interrupt (^X) */
  187. void
  188. onintr(c)
  189. {
  190.     signal(SIGINT, SIG_IGN);
  191.     longjmp(intrjmp, -1);
  192. }
  193.  
  194. STATIC int Zctlesc;    /* Encode control characters */
  195. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  196. STATIC char *Progname = "sz";
  197. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  198.  
  199. /*
  200.  * Log an error
  201.  */
  202. /*VARARGS1*/
  203. void
  204. zperr(s,p,u)
  205. char *s, *p, *u;
  206. {
  207.     if (Verbose <= 0)
  208.         return;
  209.     fprintf(stderr, "Retry %d: ", errors);
  210.     fprintf(stderr, s, p, u);
  211.     fprintf(stderr, "\n");
  212. }
  213.  
  214. #include "zm.c"
  215. #include "zmr.c"
  216.  
  217. main(argc, argv)
  218. char *argv[];
  219. {
  220.     register char *cp;
  221.     register npats;
  222.     int dm;
  223.     char **patts;
  224.  
  225.     if ((cp = getenv("ZNULLS")) && *cp)
  226.         Znulls = atoi(cp);
  227.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  228.         Restricted=TRUE;
  229.     inittty();
  230.     chkinvok(argv[0]);
  231.  
  232.     Rxtimeout = 600;
  233.     npats=0;
  234.     if (argc<2)
  235.         usage();
  236.     while (--argc) {
  237.         cp = *++argv;
  238.         if (*cp++ == '-' && *cp) {
  239.             while ( *cp) {
  240.                 switch(*cp++) {
  241.                 case '\\':
  242.                      *cp = toupper(*cp);  continue;
  243.                 case '+':
  244.                     Lzmanag = ZMAPND; break;
  245. #ifdef CSTOPB
  246.                 case '2':
  247.                     Twostop = TRUE; break;
  248. #endif
  249.                 case 'a':
  250.                     if (Nozmodem || Modem2)
  251.                         usage;
  252.                     Lzconv = ZCNL;  break;
  253.                 case 'b':
  254.                     Lzconv = ZCBIN; break;
  255.                 case 'C':
  256.                     if (--argc < 1) {
  257.                         usage();
  258.                     }
  259.                     Cmdtries = atoi(*++argv);
  260.                     break;
  261.                 case 'c':
  262.                     Lzmanag = ZMCHNG;  break;
  263.                 case 'd':
  264.                     ++Dottoslash;
  265.                     /* **** FALL THROUGH TO **** */
  266.                 case 'f':
  267.                     Fullname=TRUE; break;
  268.                 case 'e':
  269.                     Zctlesc = 1; break;
  270.                 case 'k':
  271.                     blklen=1024; break;
  272.                 case 'L':
  273.                     if (--argc < 1) {
  274.                         usage();
  275.                     }
  276.                     blkopt = atoi(*++argv);
  277.                     if (blkopt<24 || blkopt>1024)
  278.                         usage();
  279.                     break;
  280.                 case 'l':
  281.                     if (--argc < 1) {
  282.                         usage();
  283.                     }
  284.                     Tframlen = atoi(*++argv);
  285.                     if (Tframlen<32 || Tframlen>1024)
  286.                         usage();
  287.                     break;
  288.                 case 'N':
  289.                     Lzmanag = ZMNEWL;  break;
  290.                 case 'n':
  291.                     Lzmanag = ZMNEW;  break;
  292.                 case 'o':
  293.                     Wantfcs32 = FALSE; break;
  294.                 case 'p':
  295.                     Lzmanag = ZMPROT;  break;
  296.                 case 'r':
  297.                     if (Lzconv == ZCRESUM)
  298.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  299.                     Lzconv = ZCRESUM; break;
  300.                 case 't':
  301.                     if (--argc < 1) {
  302.                         usage();
  303.                     }
  304.                     Rxtimeout = atoi(*++argv);
  305.                     if (Rxtimeout<10 || Rxtimeout>1000)
  306.                         usage();
  307.                     break;
  308.                 case 'T':
  309.                     chartest(1); chartest(2);
  310.                     mode(0);  exit(0);
  311.                 case 'u':
  312.                     ++Unlinkafter; break;
  313.                 case 'v':
  314.                     ++Verbose; break;
  315.                 case 'w':
  316.                     if (--argc < 1) {
  317.                         usage();
  318.                     }
  319.                     Txwindow = atoi(*++argv);
  320.                     if (Txwindow < 256)
  321.                         Txwindow = 256;
  322.                     Txwindow = (Txwindow/64) * 64;
  323.                     Txwspac = Txwindow/4;
  324.                     if (blkopt > Txwspac
  325.                      || (!blkopt && Txwspac < 1024))
  326.                         blkopt = Txwspac;
  327.                     break;
  328.                 case 'Y':
  329.                     Lskipnocor = TRUE;
  330.                     /* **** FALLL THROUGH TO **** */
  331.                 case 'y':
  332.                     Lzmanag = ZMCLOB; break;
  333.                 case 'Z':
  334.                 case 'z':
  335.                     Lztrans = ZTRLE;  break;
  336.                 default:
  337.                     usage();
  338.                 }
  339.             }
  340.         }
  341.         else if (Command) {
  342.             if (argc != 1) {
  343.                 usage();
  344.             }
  345.             Cmdstr = *argv;
  346.         }
  347.         else if ( !npats && argc>0) {
  348.             if (argv[0][0]) {
  349.                 npats=argc;
  350.                 patts=argv;
  351.             }
  352.         }
  353.     }
  354.     if (npats < 1 && !Command && !Test) 
  355.         usage();
  356.     if (Verbose) {
  357.         if (freopen(LOGFILE, "a", stderr)==NULL)
  358.             if (freopen(LOGFILE2, "a", stderr)==NULL) {
  359.                 printf("Can't open log file!");
  360.                 exit(2);
  361.             }
  362.         setbuf(stderr, NULL);
  363.     }
  364.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  365.  
  366.     mode(3);
  367.  
  368.     if (signal(SIGINT, bibi) == SIG_IGN) {
  369.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  370.     } else {
  371.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  372.     }
  373. #ifdef SIGQUIT
  374.     signal(SIGQUIT, SIG_IGN);
  375. #endif
  376. #ifdef SIGTERM
  377.     signal(SIGTERM, bibi);
  378. #endif
  379.  
  380.     if ( !Modem2) {
  381.         if (!Nozmodem) {
  382.             printf("rz\r");  fflush(stdout);
  383.         }
  384.         countem(npats, patts);
  385.         if (!Nozmodem) {
  386.             stohdr(0L);
  387.             if (Command)
  388.                 Txhdr[ZF0] = ZCOMMAND;
  389.             zshhdr(4, ZRQINIT, Txhdr);
  390.         }
  391.     }
  392.     fflush(stdout);
  393.  
  394.     if (Command) {
  395.         if (getzrxinit()) {
  396.             Exitcode=1; canit();
  397.         }
  398.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  399.             Exitcode=1; canit();
  400.         }
  401.     } else if (wcsend(npats, patts)==ERROR) {
  402.         Exitcode=1;
  403.         canit();
  404.     }
  405.     if (endmsg[0]) {
  406.         printf("  %s: %s\r\n", Progname, endmsg);
  407.         if (Verbose)
  408.             fprintf(stderr, "%s\r\n", endmsg);
  409.     }
  410.     printf("%s %s finished.\r\n", Progname, VERSION);
  411.     fflush(stdout);
  412.     mode(0);
  413.     if(errcnt || Exitcode)
  414.         exit(1);
  415.  
  416. #ifndef REGISTERED
  417.     /* Removing or disabling this code without registering is theft */
  418.     if (!Usevhdrs)  {
  419.         printf("\n\n\nPlease read the License Agreement in sz.doc\n");
  420.         fflush(stdout);
  421.         sleep(10);
  422.     }
  423. #endif
  424.     exit(0);
  425.     /*NOTREACHED*/
  426. }
  427.  
  428. /* Say "bibi" to the receiver, try to do it cleanly */
  429. void
  430. saybibi()
  431. {
  432.     for (;;) {
  433.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  434.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  435.         switch (zgethdr(Rxhdr, 0)) {
  436.         case ZFIN:
  437.             sendline('O'); sendline('O'); flushmo();
  438.         case ZCAN:
  439.         case TIMEOUT:
  440.             return;
  441.         }
  442.     }
  443. }
  444.  
  445. wcsend(argc, argp)
  446. char *argp[];
  447. {
  448.     register n;
  449.  
  450.     Crcflg=FALSE;
  451.     firstsec=TRUE;
  452.     bytcnt = -1;
  453.     if (Nozmodem) {
  454.         printf("Start your YMODEM receive. ");  fflush(stdout);
  455.     }
  456.     for (n=0; n<argc; ++n) {
  457.         Totsecs = 0;
  458.         if (wcs(argp[n])==ERROR)
  459.             return ERROR;
  460.     }
  461.     Totsecs = 0;
  462.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  463.         if (!Nozmodem && !Modem2) {
  464.             Command = TRUE;
  465.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  466.             if (getnak()) {
  467.                 Exitcode=1; canit();
  468.             }
  469.             if (!Zmodem)
  470.                 canit();
  471.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  472.                 Exitcode=1; canit();
  473.             }
  474.             Exitcode = 1; return OK;
  475.         }
  476.         canit();
  477.         sprintf(endmsg, "Can't open any requested files");
  478.         return ERROR;
  479.     }
  480.     if (Zmodem)
  481.         saybibi();
  482.     else if ( !Modem2)
  483.         wctxpn("");
  484.     return OK;
  485. }
  486.  
  487. wcs(oname)
  488. char *oname;
  489. {
  490.     register c;
  491.     register char *p, *q;
  492.     struct stat f;
  493.     char name[PATHLEN];
  494.  
  495.     strcpy(name, oname);
  496.  
  497.     if (Restricted) {
  498.         /* restrict pathnames to current tree or uucppublic */
  499.         if ( substr(name, "../")
  500.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  501.             canit();  sprintf(endmsg,"Security Violation");
  502.             return ERROR;
  503.         }
  504.     }
  505.  
  506.     in=fopen(oname, "r");
  507.  
  508.     if (in==NULL) {
  509.         ++errcnt;
  510.         return OK;    /* pass over it, there may be others */
  511.     }
  512.     BEofseen = Eofseen = 0;  vpos = 0;
  513.  
  514.     /* Check for directory */
  515.     fstat(fileno(in), &f);
  516. #ifdef POSIX
  517.     if (S_ISDIR(f.st_mode))
  518. #else
  519.     c = f.st_mode & S_IFMT;
  520.     if (c == S_IFDIR || c == S_IFBLK)
  521. #endif
  522.     {
  523.         fclose(in);
  524.         return OK;
  525.     }
  526.  
  527.     ++Filcnt;
  528.     switch (wctxpn(name)) {
  529.     case ZSKIP:
  530.     case ZFERR:
  531.         return OK;
  532.     case OK:
  533.         break;
  534.     default:
  535.         return ERROR;
  536.     }
  537.     if (!Zmodem && wctx(f.st_size))
  538.         return ERROR;
  539.  
  540.     if (Unlinkafter)
  541.         unlink(oname);
  542.  
  543.     return 0;
  544. }
  545.  
  546. /*
  547.  * generate and transmit pathname block consisting of
  548.  *  pathname (null terminated),
  549.  *  file length, mode time and file mode in octal
  550.  *  as provided by the Unix fstat call.
  551.  *  N.B.: modifies the passed name, may extend it!
  552.  */
  553. wctxpn(name)
  554. char *name;
  555. {
  556.     register char *p, *q;
  557.     char name2[PATHLEN];
  558.     struct stat f;
  559.  
  560.     if (Modem2) {
  561.         if (*name && fstat(fileno(in), &f)!= -1) {
  562.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  563.               name, (127+f.st_size)>>7);
  564.         }
  565.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  566.         fflush(stderr);
  567.         return OK;
  568.     }
  569.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  570.     if ( !Zmodem)
  571.         if (getnak())
  572.             return ERROR;
  573.  
  574.     q = (char *) 0;
  575.     if (Dottoslash) {        /* change . to . */
  576.         for (p=name; *p; ++p) {
  577.             if (*p == '/')
  578.                 q = p;
  579.             else if (*p == '.')
  580.                 *(q=p) = '/';
  581.         }
  582.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  583.             q += 8;            /*   make it .ext */
  584.             strcpy(name2, q);    /* save excess of name */
  585.             *q = '.';
  586.             strcpy(++q, name2);    /* add it back */
  587.         }
  588.     }
  589.  
  590.     for (p=name, q=txbuf ; *p; )
  591.         if ((*q++ = *p++) == '/' && !Fullname)
  592.             q = txbuf;
  593.     *q++ = 0;
  594.     p=q;
  595.     while (q < (txbuf + 1024))
  596.         *q++ = 0;
  597.     if (*name) {
  598.         if (fstat(fileno(in), &f)!= -1)
  599.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  600.               f.st_mode, Filesleft, Totalleft);
  601.         Totalleft -= f.st_size;
  602.     }
  603.     if (--Filesleft <= 0)
  604.         Filesleft = Totalleft = 0;
  605.     if (Totalleft < 0)
  606.         Totalleft = 0;
  607.  
  608.     /* force 1k blocks if name won't fit in 128 byte block */
  609.     if (txbuf[125])
  610.         blklen=1024;
  611.     else {        /* A little goodie for IMP/KMD */
  612.         txbuf[127] = (f.st_size + 127) >>7;
  613.         txbuf[126] = (f.st_size + 127) >>15;
  614.     }
  615.     if (Zmodem)
  616.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  617.     if (wcputsec(txbuf, 0, 128)==ERROR)
  618.         return ERROR;
  619.     return OK;
  620. }
  621.  
  622. getnak()
  623. {
  624.     register firstch;
  625.  
  626.     Lastrx = 0;
  627.     for (;;) {
  628.         switch (firstch = readline(800)) {
  629.         case ZPAD:
  630.             if (getzrxinit())
  631.                 return ERROR;
  632.             return FALSE;
  633.         case TIMEOUT:
  634.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  635.             return TRUE;
  636.         case WANTG:
  637. #ifdef MODE2OK
  638.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  639. #endif
  640.             Optiong = TRUE;
  641.             blklen=1024;
  642.         case WANTCRC:
  643.             Crcflg = TRUE;
  644.         case NAK:
  645.             return FALSE;
  646.         case CAN:
  647.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  648.                 sprintf(endmsg, "Got CAN waiting to send file");
  649.                 return TRUE;
  650.             }
  651.         default:
  652.             break;
  653.         }
  654.         Lastrx = firstch;
  655.     }
  656. }
  657.  
  658.  
  659. wctx(flen)
  660. unsigned long flen;
  661. {
  662.     register int thisblklen;
  663.     register int sectnum, attempts, firstch;
  664.     unsigned long charssent;
  665.  
  666.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  667.     vfile("wctx:file length=%ld", flen);
  668.  
  669.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  670.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  671.         ;
  672.     if (firstch==CAN) {
  673.         zperr("Receiver CANcelled");
  674.         return ERROR;
  675.     }
  676.     if (firstch==WANTCRC)
  677.         Crcflg=TRUE;
  678.     if (firstch==WANTG)
  679.         Crcflg=TRUE;
  680.     sectnum=0;
  681.     for (;;) {
  682.         if (flen <= (charssent + 896L))
  683.             thisblklen = 128;
  684.         if ( !filbuf(txbuf, thisblklen))
  685.             break;
  686.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  687.             return ERROR;
  688.         charssent += thisblklen;
  689.     }
  690.     fclose(in);
  691.     attempts=0;
  692.     do {
  693.         purgeline();
  694.         sendline(EOT);
  695.         flushmo();
  696.         ++attempts;
  697.     }
  698.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  699.     if (attempts == RETRYMAX) {
  700.         zperr("No ACK on EOT");
  701.         return ERROR;
  702.     }
  703.     else
  704.         return OK;
  705. }
  706.  
  707. wcputsec(buf, sectnum, cseclen)
  708. char *buf;
  709. int sectnum;
  710. int cseclen;    /* data length of this sector to send */
  711. {
  712.     register checksum, wcj;
  713.     register char *cp;
  714.     unsigned oldcrc;
  715.     int firstch;
  716.     int attempts;
  717.  
  718.     firstch=0;    /* part of logic to detect CAN CAN */
  719.  
  720.     if (Verbose>2)
  721.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  722.     else if (Verbose>1)
  723.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  724.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  725.         Lastrx= firstch;
  726.         sendline(cseclen==1024?STX:SOH);
  727.         sendline(sectnum);
  728.         sendline(-sectnum -1);
  729.         oldcrc=checksum=0;
  730.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  731.             sendline(*cp);
  732.             oldcrc=updcrc((0377& *cp), oldcrc);
  733.             checksum += *cp++;
  734.         }
  735.         if (Crcflg) {
  736.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  737.             sendline((int)oldcrc>>8);
  738.             sendline((int)oldcrc);
  739.         }
  740.         else
  741.             sendline(checksum);
  742.         flushmo();
  743.  
  744.         if (Optiong) {
  745.             firstsec = FALSE; return OK;
  746.         }
  747.         firstch = readline(Rxtimeout);
  748. gotnak:
  749.         switch (firstch) {
  750.         case CAN:
  751.             if(Lastrx == CAN) {
  752. cancan:
  753.                 zperr("Cancelled");  return ERROR;
  754.             }
  755.             break;
  756.         case TIMEOUT:
  757.             zperr("Timeout on sector ACK"); continue;
  758.         case WANTCRC:
  759.             if (firstsec)
  760.                 Crcflg = TRUE;
  761.         case NAK:
  762.             zperr("NAK on sector"); continue;
  763.         case ACK: 
  764.             firstsec=FALSE;
  765.             Totsecs += (cseclen>>7);
  766.             return OK;
  767.         case ERROR:
  768.             zperr("Got burst for sector ACK"); break;
  769.         default:
  770.             zperr("Got %02x for sector ACK", firstch); break;
  771.         }
  772.         for (;;) {
  773.             Lastrx = firstch;
  774.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  775.                 break;
  776.             if (firstch == NAK || firstch == WANTCRC)
  777.                 goto gotnak;
  778.             if (firstch == CAN && Lastrx == CAN)
  779.                 goto cancan;
  780.         }
  781.     }
  782.     zperr("Retry Count Exceeded");
  783.     return ERROR;
  784. }
  785.  
  786. /* fill buf with count chars padding with ^Z for CPM */
  787. filbuf(buf, count)
  788. register char *buf;
  789. {
  790.     register c, m;
  791.  
  792.     m = read(fileno(in), buf, count);
  793.     if (m <= 0)
  794.         return 0;
  795.     while (m < count)
  796.         buf[m++] = 032;
  797.     return count;
  798. }
  799.  
  800. /* Fill buffer with blklen chars */
  801. zfilbuf()
  802. {
  803.     int n;
  804.  
  805. #ifdef TXBSIZE
  806.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  807.     /* We assume request is within buffer, or just beyond */
  808.     txbuf = Txb + (bytcnt & TXBMASK);
  809.     if (vpos <= bytcnt) {
  810.         n = fread(txbuf, 1, blklen, in);
  811.  
  812.         vpos += n;
  813.         if (n < blklen)
  814.             Eofseen = 1;
  815.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  816.         return n;
  817.     }
  818.     if (vpos >= (bytcnt+blklen))
  819.         return blklen;
  820.     /* May be a short block if crash recovery etc. */
  821.     Eofseen = BEofseen;
  822.     return (vpos - bytcnt);
  823. #else
  824.     n = fread(txbuf, 1, blklen, in);
  825.     if (n < blklen)
  826.         Eofseen = 1;
  827.     return n;
  828. #endif
  829. }
  830.  
  831. #ifdef TXBSIZE
  832. /* Replacement for brain damaged fseek function.  Returns 0==success */
  833. fooseek(fptr, pos, whence)
  834. FILE *fptr;
  835. unsigned long pos;
  836. {
  837.     unsigned long m, n;
  838.  
  839.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  840.     /* Seek offset < current buffer */
  841.     if (pos < (vpos -TXBSIZE +1024)) {
  842.         BEofseen = 0;
  843.         if (Canseek > 0) {
  844.             vpos = pos & ~TXBMASK;
  845.             if (vpos >= pos)
  846.                 vpos -= TXBSIZE;
  847.             if (fseek(fptr, vpos, 0))
  848.                 return 1;
  849.         }
  850.         else if (Canseek == 0) {
  851.             if (fseek(fptr, vpos = 0L, 0))
  852.                 return 1;
  853.         } else
  854.             return 1;
  855.         while (vpos < pos) {
  856.             n = fread(Txb, 1, TXBSIZE, fptr);
  857.             vpos += n;
  858.             vfile("n=%d vpos=%ld", n, vpos);
  859.             if (n < TXBSIZE) {
  860.                 BEofseen = 1;
  861.                 break;
  862.             }
  863.         }
  864.         vfile("vpos=%ld", vpos);
  865.         return 0;
  866.     }
  867.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  868.     if (pos > vpos) {
  869.         if (Canseek)
  870.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  871.                 return 1;
  872.         while (vpos <= pos) {
  873.             txbuf = Txb + (vpos & TXBMASK);
  874.             m = TXBSIZE - (vpos & TXBMASK);
  875.             vfile("m=%ld vpos=%ld", m,vpos);
  876.                 n = fread(txbuf, 1, m, fptr);
  877.             vfile("n=%ld vpos=%ld", n,vpos);
  878.             vpos += n;
  879.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  880.             if (n < m) {
  881.                 BEofseen = 1;
  882.                 break;
  883.             }
  884.         }
  885.         return 0;
  886.     }
  887.     /* Seek offset is within current buffer */
  888.     vfile("within buffer: vpos=%ld", vpos);
  889.     return 0;
  890. }
  891. #define fseek fooseek
  892. #endif
  893.  
  894.  
  895. /*
  896.  * substr(string, token) searches for token in string s
  897.  * returns pointer to token within string if found, NULL otherwise
  898.  */
  899. char *
  900. substr(s, t)
  901. register char *s,*t;
  902. {
  903.     register char *ss,*tt;
  904.     /* search for first char of token */
  905.     for (ss=s; *s; s++)
  906.         if (*s == *t)
  907.             /* compare token with substring */
  908.             for (ss=s,tt=t; ;) {
  909.                 if (*tt == 0)
  910.                     return s;
  911.                 if (*ss++ != *tt++)
  912.                     break;
  913.             }
  914.     return NULL;
  915. }
  916.  
  917. char *usinfo[] = {
  918.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  919.     "Usage:    sz [-2+abcdefklLnNuvwyY] [-] file ...",
  920.     "\t    zcommand [-2Cev] COMMAND",
  921.     "\t    zcommandi [-2Cev] COMMAND",
  922.     "\t    sb [-2adfkuv] [-] file ...",
  923.     "\t    sx [-2akuv] [-] file",
  924.     ""
  925. };
  926.  
  927. usage()
  928. {
  929.     char **pp;
  930.  
  931.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  932.      Progname, VERSION, OS);
  933.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  934.     for (pp=usinfo; **pp; ++pp)
  935.         fprintf(stderr, "%s\n", *pp);
  936.     fprintf(stderr,"\nCopyright 1993 Omen Technology INC All Rights Reserved\n");
  937.     fprintf(stderr,
  938.      "See sz.doc for option descriptions and licensing information.\n\n");
  939.     exit(3);
  940. }
  941.  
  942. /*
  943.  * Get the receiver's init parameters
  944.  */
  945. getzrxinit()
  946. {
  947.     register n;
  948.     struct stat f;
  949.  
  950.     for (n=10; --n>=0; ) {
  951.         
  952.         switch (zgethdr(Rxhdr, 1)) {
  953.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  954.             stohdr(Rxpos);
  955.             zshhdr(4, ZACK, Txhdr);
  956.             continue;
  957.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  958.             stohdr(0L);
  959.             zshhdr(4, ZRQINIT, Txhdr);
  960.             continue;
  961.         case ZRINIT:
  962.             Rxflags = 0377 & Rxhdr[ZF0];
  963.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  964.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  965.             Zctlesc |= Rxflags & TESCCTL;
  966.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  967.             if ( !(Rxflags & CANFDX))
  968.                 Txwindow = 0;
  969.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  970.             signal(SIGINT, SIG_IGN);
  971. #ifdef MODE2OK
  972.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  973. #endif
  974.  
  975. #ifndef READCHECK
  976. #ifndef USG
  977.             /* Use 1024 byte frames if no sample/interrupt */
  978.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  979.                 Rxbuflen = 1024;
  980.                 vfile("Rxbuflen=%d", Rxbuflen);
  981.             }
  982. #endif
  983. #endif
  984.  
  985.             /* Override to force shorter frame length */
  986.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  987.                 Rxbuflen = Tframlen;
  988.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  989.                 Rxbuflen = Tframlen;
  990.             vfile("Rxbuflen=%d", Rxbuflen);
  991.  
  992.  
  993.             /*
  994.              * If input is not a regular file, force ACK's to
  995.              *  prevent running beyond the buffer limits
  996.              */
  997.             if ( !Command) {
  998.                 fstat(fileno(in), &f);
  999.                 if (
  1000. #ifdef POSIX
  1001.                     !S_ISREG(f.st_mode)
  1002. #else
  1003.                     (f.st_mode & S_IFMT) != S_IFREG
  1004. #endif
  1005.                     ) {
  1006.                     Canseek = -1;
  1007. #ifdef TXBSIZE
  1008.                     Txwindow = TXBSIZE - 1024;
  1009.                     Txwspac = TXBSIZE/4;
  1010. #else
  1011.                     return ERROR;
  1012. #endif
  1013.                 }
  1014.             }
  1015.  
  1016.             /* Set initial subpacket length */
  1017.             if (blklen < 1024) {    /* Command line override? */
  1018.                 if (Effbaud > 300)
  1019.                     blklen = 256;
  1020.                 if (Effbaud > 1200)
  1021.                     blklen = 512;
  1022.                 if (Effbaud > 2400)
  1023.                     blklen = 1024;
  1024.             }
  1025.             if (Rxbuflen && blklen>Rxbuflen)
  1026.                 blklen = Rxbuflen;
  1027.             if (blkopt && blklen > blkopt)
  1028.                 blklen = blkopt;
  1029.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1030.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1031.  
  1032.  
  1033.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1034.                 Txfcs32 = 2;
  1035.             else
  1036.                 Lztrans = 0;
  1037.  
  1038.             return (sendzsinit());
  1039.         case ZCAN:
  1040.         case TIMEOUT:
  1041.             return ERROR;
  1042.         case ZRQINIT:
  1043.             if (Rxhdr[ZF0] == ZCOMMAND)
  1044.                 continue;
  1045.         default:
  1046.             zshhdr(4, ZNAK, Txhdr);
  1047.             continue;
  1048.         }
  1049.     }
  1050.     return ERROR;
  1051. }
  1052.  
  1053. /* Send send-init information */
  1054. sendzsinit()
  1055. {
  1056.     register c;
  1057.  
  1058.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1059.         return OK;
  1060.     errors = 0;
  1061.     for (;;) {
  1062.         stohdr(0L);
  1063. #ifdef ALTCANOFF
  1064.         Txhdr[ALTCOFF] = ALTCANOFF;
  1065. #endif
  1066.         if (Zctlesc) {
  1067.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1068.         }
  1069.         else
  1070.             zsbhdr(4, ZSINIT, Txhdr);
  1071.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1072.         c = zgethdr(Rxhdr, 1);
  1073.         switch (c) {
  1074.         case ZCAN:
  1075.             return ERROR;
  1076.         case ZACK:
  1077.             return OK;
  1078.         default:
  1079.             if (++errors > 19)
  1080.                 return ERROR;
  1081.             continue;
  1082.         }
  1083.     }
  1084. }
  1085.  
  1086. /* Send file name and related info */
  1087. zsendfile(buf, blen)
  1088. char *buf;
  1089. {
  1090.     register c;
  1091.     register unsigned long crc;
  1092.     long lastcrcrq = -1;
  1093.     char *p;
  1094.  
  1095.     for (errors=0; ++errors<11;) {
  1096.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1097.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1098.         if (Lskipnocor)
  1099.             Txhdr[ZF1] |= ZMSKNOLOC;
  1100.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1101.         Txhdr[ZF3] = 0;
  1102.         zsbhdr(4, ZFILE, Txhdr);
  1103.         zsdata(buf, blen, ZCRCW);
  1104. again:
  1105.         c = zgethdr(Rxhdr, 1);
  1106.         switch (c) {
  1107.         case ZRINIT:
  1108.             while ((c = readline(50)) > 0)
  1109.                 if (c == ZPAD) {
  1110.                     goto again;
  1111.                 }
  1112.             continue;
  1113.         case ZCAN:
  1114.         case TIMEOUT:
  1115.         case ZABORT:
  1116.         case ZFIN:
  1117.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1118.             return ERROR;
  1119.         default:
  1120.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1121.             continue;
  1122.         case ERROR:
  1123.         case ZNAK:
  1124.             continue;
  1125.         case ZCRC:
  1126.             if (Rxpos != lastcrcrq) {
  1127.                 lastcrcrq = Rxpos;
  1128.                 crc = 0xFFFFFFFFL;
  1129.                 if (Canseek >= 0) {
  1130.                     fseek(in, 0L, 0);
  1131.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1132.                         crc = UPDC32(c, crc);
  1133.                     crc = ~crc;
  1134.                     clearerr(in);    /* Clear possible EOF */
  1135.                     lastcrcrq = Rxpos;
  1136.                 }
  1137.             }
  1138.             stohdr(crc);
  1139.             zsbhdr(4, ZCRC, Txhdr);
  1140.             goto again;
  1141.         case ZFERR:
  1142.         case ZSKIP:
  1143.             sprintf(endmsg, "File skipped by receiver request");
  1144.             fclose(in); return c;
  1145.         case ZRPOS:
  1146.             /*
  1147.              * Suppress zcrcw request otherwise triggered by
  1148.              * lastyunc==bytcnt
  1149.              */
  1150.             if (fseek(in, Rxpos, 0))
  1151.                 return ERROR;
  1152.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1153.             return zsendfdata();
  1154.         }
  1155.     }
  1156.     fclose(in); return ERROR;
  1157. }
  1158.  
  1159. /* Send the data in the file */
  1160. zsendfdata()
  1161. {
  1162.     register c, e, n;
  1163.     register newcnt;
  1164.     register long tcount = 0;
  1165.     int junkcount;        /* Counts garbage chars received by TX */
  1166.     static int tleft = 6;    /* Counter for test mode */
  1167.  
  1168.     junkcount = 0;
  1169.     Beenhereb4 = FALSE;
  1170. somemore:
  1171.     if (setjmp(intrjmp)) {
  1172. waitack:
  1173.         junkcount = 0;
  1174.         c = getinsync(0);
  1175. gotack:
  1176.         switch (c) {
  1177.         default:
  1178.         case ZCAN:
  1179.             fclose(in);
  1180.             return ERROR;
  1181.         case ZRINIT:
  1182.             fclose(in);
  1183.             return ZSKIP;
  1184.         case ZSKIP:
  1185.             fclose(in);
  1186.             return c;
  1187.         case ZACK:
  1188.         case ZRPOS:
  1189.             break;
  1190.         }
  1191. #ifdef READCHECK
  1192.         /*
  1193.          * If the reverse channel can be tested for data,
  1194.          *  this logic may be used to detect error packets
  1195.          *  sent by the receiver, in place of setjmp/longjmp
  1196.          *  rdchk(Tty) returns non 0 if a character is available
  1197.          */
  1198.         while (rdchk(Tty)) {
  1199. #ifdef EATSIT
  1200.             switch (checked)
  1201. #else
  1202.             switch (readline(1))
  1203. #endif
  1204.             {
  1205.             case CAN:
  1206.             case ZPAD:
  1207.                 c = getinsync(1);
  1208.                 goto gotack;
  1209.             case XOFF:        /* Wait a while for an XON */
  1210.             case XOFF|0200:
  1211.                 readline(100);
  1212.             }
  1213.         }
  1214. #endif
  1215.     }
  1216.  
  1217.     signal(SIGINT, onintr);
  1218.     newcnt = Rxbuflen;
  1219.     Txwcnt = 0;
  1220.     stohdr(Txpos);
  1221.     zsbhdr(4, ZDATA, Txhdr);
  1222.  
  1223.     /*
  1224.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1225.      *  many times.  Each time the signal should be caught, causing the
  1226.      *  file to be started over from the beginning.
  1227.      */
  1228.     if (Test) {
  1229.         if ( --tleft)
  1230.             while (tcount < 20000) {
  1231.                 printf(qbf); fflush(stdout);
  1232.                 tcount += strlen(qbf);
  1233. #ifdef READCHECK
  1234.                 while (rdchk(Tty)) {
  1235. #ifdef EATSIT
  1236.                     switch (checked)
  1237. #else
  1238.                     switch (readline(1))
  1239. #endif
  1240.                     {
  1241.                     case CAN:
  1242.                     case ZPAD:
  1243.                         goto waitack;
  1244.                     case XOFF:    /* Wait for XON */
  1245.                     case XOFF|0200:
  1246.                         readline(100);
  1247.                     }
  1248.                 }
  1249. #endif
  1250.             }
  1251.         signal(SIGINT, SIG_IGN); canit();
  1252.         sleep(3); purgeline(); mode(0);
  1253.         printf("\nsz: Tcount = %ld\n", tcount);
  1254.         if (tleft) {
  1255.             printf("ERROR: Interrupts Not Caught\n");
  1256.             exit(1);
  1257.         }
  1258.         exit(0);
  1259.     }
  1260.  
  1261.     do {
  1262.         n = zfilbuf();
  1263.         if (Eofseen)
  1264.             e = ZCRCE;
  1265.         else if (junkcount > 3)
  1266.             e = ZCRCW;
  1267.         else if (bytcnt == Lastsync)
  1268.             e = ZCRCW;
  1269.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1270.             e = ZCRCW;
  1271.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1272.             Txwcnt = 0;  e = ZCRCQ;
  1273.         } else
  1274.             e = ZCRCG;
  1275.         if (Verbose>1)
  1276.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1277.               Txpos, Crc32t?" CRC-32":"");
  1278.         zsdata(txbuf, n, e);
  1279.         bytcnt = Txpos += n;
  1280.         if (e == ZCRCW)
  1281.             goto waitack;
  1282. #ifdef READCHECK
  1283.         /*
  1284.          * If the reverse channel can be tested for data,
  1285.          *  this logic may be used to detect error packets
  1286.          *  sent by the receiver, in place of setjmp/longjmp
  1287.          *  rdchk(Tty) returns non 0 if a character is available
  1288.          */
  1289.         fflush(stdout);
  1290.         while (rdchk(Tty)) {
  1291. #ifdef EATSIT
  1292.             switch (checked)
  1293. #else
  1294.             switch (readline(1))
  1295. #endif
  1296.             {
  1297.             case CAN:
  1298.             case ZPAD:
  1299.                 c = getinsync(1);
  1300.                 if (c == ZACK)
  1301.                     break;
  1302.                 /* zcrce - dinna wanna starta ping-pong game */
  1303.                 zsdata(txbuf, 0, ZCRCE);
  1304.                 goto gotack;
  1305.             case XOFF:        /* Wait a while for an XON */
  1306.             case XOFF|0200:
  1307.                 readline(100);
  1308.             default:
  1309.                 ++junkcount;
  1310.             }
  1311.         }
  1312. #endif    /* READCHECK */
  1313.         if (Txwindow) {
  1314.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1315.                 vfile("%ld window >= %u", tcount, Txwindow);
  1316.                 if (e != ZCRCQ)
  1317.                     zsdata(txbuf, 0, e = ZCRCQ);
  1318.                 c = getinsync(1);
  1319.                 if (c != ZACK) {
  1320.                     zsdata(txbuf, 0, ZCRCE);
  1321.                     goto gotack;
  1322.                 }
  1323.             }
  1324.             vfile("window = %ld", tcount);
  1325.         }
  1326.     } while (!Eofseen);
  1327.     signal(SIGINT, SIG_IGN);
  1328.  
  1329.     if (Lrxpos != Txpos)
  1330.         n = Unlinkafter | Txwindow;
  1331.     else
  1332.         n = 0;
  1333.     for (;;) {
  1334.         stohdr(Txpos);
  1335.         if (n) {
  1336.             zsbhdr(4, ZDATA, Txhdr);
  1337.             zsdata(txbuf, 0, ZCRCW);
  1338.         } else
  1339.             zsbhdr(4, ZEOF, Txhdr);
  1340.         switch (getinsync(0)) {
  1341.         case ZACK:
  1342.             n = 0;  continue;
  1343.         case ZNAK:
  1344.             continue;
  1345.         case ZRPOS:
  1346.             goto somemore;
  1347.         case ZRINIT:
  1348.             fclose(in);
  1349.             return OK;
  1350.         case ZSKIP:
  1351.             fclose(in);
  1352.             sprintf(endmsg, "File skipped by receiver request");
  1353.             return c;
  1354.         default:
  1355.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1356.         case ERROR:
  1357.             fclose(in);
  1358.             return ERROR;
  1359.         }
  1360.     }
  1361. }
  1362.  
  1363. /*
  1364.  * Respond to receiver's complaint, get back in sync with receiver
  1365.  */
  1366. getinsync(flag)
  1367. {
  1368.     register c;
  1369.  
  1370.     for (;;) {
  1371.         if (Test) {
  1372.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1373.             Rxpos = 0; c = ZRPOS;
  1374.         } else
  1375.             c = zgethdr(Rxhdr, 0);
  1376.         switch (c) {
  1377.         case ZCAN:
  1378.         case ZABORT:
  1379.         case ZFIN:
  1380.         case TIMEOUT:
  1381.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1382.             return ERROR;
  1383.         case ZRPOS:
  1384.             if (Rxpos > bytcnt) {
  1385.                 sprintf(endmsg, "Nonstandard Protocol");
  1386.                 return ZRPOS;
  1387.             }
  1388.             /* ************************************* */
  1389.             /*  If sending to a buffered modem, you  */
  1390.             /*   might send a break at this point to */
  1391.             /*   dump the modem's buffer.         */
  1392.             clearerr(in);    /* In case file EOF seen */
  1393.             if (fseek(in, Rxpos, 0)) {
  1394.                 sprintf(endmsg, "Bad Seek");
  1395.                 return ERROR;
  1396.             }
  1397.             Eofseen = 0;
  1398.             bytcnt = Lrxpos = Txpos = Rxpos;
  1399.             if (Lastsync == Rxpos) {
  1400.                 if (++Beenhereb4 > 12) {
  1401.                     sprintf(endmsg, "Can't send block");
  1402.                     return ERROR;
  1403.                 }
  1404.                 if (Beenhereb4 > 4)
  1405.                     if (blklen > 32)
  1406.                         blklen /= 2;
  1407.             }
  1408.             Lastsync = Rxpos;
  1409.             return c;
  1410.         case ZACK:
  1411.             Lrxpos = Rxpos;
  1412.             if (flag || Txpos == Rxpos)
  1413.                 return ZACK;
  1414.             continue;
  1415.         case ZRINIT:
  1416.             return c;
  1417.         case ZSKIP:
  1418.             sprintf(endmsg, "File skipped by receiver request");
  1419.             return c;
  1420.         case ERROR:
  1421.         default:
  1422.             zsbhdr(4, ZNAK, Txhdr);
  1423.             continue;
  1424.         }
  1425.     }
  1426. }
  1427.  
  1428.  
  1429. /* Send command and related info */
  1430. zsendcmd(buf, blen)
  1431. char *buf;
  1432. {
  1433.     register c;
  1434.     long cmdnum;
  1435.  
  1436.     cmdnum = getpid();
  1437.     errors = 0;
  1438.     for (;;) {
  1439.         stohdr(cmdnum);
  1440.         Txhdr[ZF0] = Cmdack1;
  1441.         zsbhdr(4, ZCOMMAND, Txhdr);
  1442.         zsdata(buf, blen, ZCRCW);
  1443. listen:
  1444.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1445.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1446.         c = zgethdr(Rxhdr, 1);
  1447.  
  1448.         switch (c) {
  1449.         case ZRINIT:
  1450.             goto listen;    /* CAF 8-21-87 */
  1451.         case ERROR:
  1452.         case GCOUNT:
  1453.         case TIMEOUT:
  1454.             if (++errors > Cmdtries)
  1455.                 return ERROR;
  1456.             continue;
  1457.         case ZCAN:
  1458.         case ZABORT:
  1459.         case ZFIN:
  1460.         case ZSKIP:
  1461.         case ZRPOS:
  1462.             return ERROR;
  1463.         default:
  1464.             if (++errors > 20)
  1465.                 return ERROR;
  1466.             continue;
  1467.         case ZCOMPL:
  1468.             Exitcode = Rxpos;
  1469.             saybibi();
  1470.             return OK;
  1471.         case ZRQINIT:
  1472.             vfile("******** RZ *******");
  1473.             system("rz");
  1474.             vfile("******** SZ *******");
  1475.             goto listen;
  1476.         }
  1477.     }
  1478. }
  1479.  
  1480. /*
  1481.  * If called as sb use YMODEM protocol
  1482.  */
  1483. chkinvok(s)
  1484. char *s;
  1485. {
  1486.     register char *p;
  1487.  
  1488.     p = s;
  1489.     while (*p == '-')
  1490.         s = ++p;
  1491.     while (*p)
  1492.         if (*p++ == '/')
  1493.             s = p;
  1494.     if (*s == 'v') {
  1495.         Verbose=1; ++s;
  1496.     }
  1497.     Progname = s;
  1498.     if (s[0]=='z' && s[1] == 'c') {
  1499.         Command = TRUE;
  1500.         if (s[8] == 'i')
  1501.             Cmdack1 = ZCACK1;
  1502.     }
  1503.     if (s[0]=='s' && s[1]=='b') {
  1504.         Nozmodem = TRUE; blklen=1024;
  1505.     }
  1506.     if (s[0]=='s' && s[1]=='x') {
  1507.         Modem2 = TRUE;
  1508.     }
  1509. }
  1510.  
  1511. countem(argc, argv)
  1512. register char **argv;
  1513. {
  1514.     register c;
  1515.     struct stat f;
  1516.  
  1517.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1518.         f.st_size = -1;
  1519.         if (Verbose>2) {
  1520.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1521.             fflush(stderr);
  1522.         }
  1523.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1524.             ++Filesleft;  Totalleft += f.st_size;
  1525.         }
  1526.         if (Verbose>2)
  1527.             fprintf(stderr, " %ld", f.st_size);
  1528.     }
  1529.     if (Verbose>2)
  1530.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1531.           Filesleft, Totalleft);
  1532. }
  1533.  
  1534. chartest(m)
  1535. {
  1536.     register n;
  1537.  
  1538.     mode(m);
  1539.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1540.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1541.     printf("Hit Enter.\021");  fflush(stdout);
  1542.     readline(500);
  1543.  
  1544.     for (n = 0; n < 256; ++n) {
  1545.         if (!(n%8))
  1546.             printf("\r\n");
  1547.         printf("%02x ", n);  fflush(stdout);
  1548.         sendline(n);    flushmo();
  1549.         printf("  ");  fflush(stdout);
  1550.         if (n == 127) {
  1551.             printf("Hit Enter.\021");  fflush(stdout);
  1552.             readline(500);
  1553.             printf("\r\n");  fflush(stdout);
  1554.         }
  1555.     }
  1556.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1557.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1558.  
  1559.     while (n != TIMEOUT && n != ' ') {
  1560.         n = readline(400);
  1561.         printf("%02x\r\n", n);
  1562.         fflush(stdout);
  1563.     }
  1564.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1565.     fflush(stdout);
  1566. }
  1567.  
  1568. /* End of sz.c */
  1569.